package com.lanf.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanf.business.model.Tbmedicines;
import com.lanf.business.vo.TbmedicinesQueryVo;
import com.lanf.business.vo.TbmedicinesExportVo;
import com.lanf.business.mapper.TbmedicinesMapper;
import com.lanf.business.service.TbmedicinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.BeanUtils;
import com.lanf.common.result.ResultCodeEnum;
import com.lanf.system.exception.LanfException;
import com.lanf.system.easyexcel.ExcelHandler;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
* @author tanlingfei
* @version 1.0
* @description 药品信息 Service实现类
* @date 2024-04-19 21:36:09
*/
@Transactional
@Service
public class TbmedicinesServiceImpl extends ServiceImpl
<TbmedicinesMapper, Tbmedicines> implements TbmedicinesService {
    @Autowired
    private TbmedicinesMapper tbmedicinesMapper;
    @Resource
    private ExcelHandler excelHandler;

    @Override
    public IPage<Tbmedicines> selectPage(Page<Tbmedicines> pageParam, TbmedicinesQueryVo tbmedicinesQueryVo) {
        //QueryWrapper<Tbmedicines> queryWrapper = new QueryWrapper<>();
        //return tbmedicinesMapper.selectPage(pageParam,queryWrapper);
        return tbmedicinesMapper.selectPage(pageParam,tbmedicinesQueryVo);
    }

    @Override
    public List<Tbmedicines> queryList(TbmedicinesQueryVo tbmedicinesQueryVo){
     List<Tbmedicines> result = tbmedicinesMapper.queryList(tbmedicinesQueryVo);
     return result;
   }

    @Override
    public boolean save(Tbmedicines tbmedicines){
        int result = this.tbmedicinesMapper.insert(tbmedicines);
        return result>0;
    }
    @Override
    public boolean updateById(Tbmedicines tbmedicines){
        int row = this.tbmedicinesMapper.updateById(tbmedicines);
        if(row <= 0){
            throw new LanfException(ResultCodeEnum.UPDATE_ERROR);
         }
         return row>0;
    }
    @Override
    public Tbmedicines getById(String id){
         Tbmedicines tbmedicines = tbmedicinesMapper.selectById(id);
         return tbmedicines;
    }
   @Override
   public List<Tbmedicines> getByIds(List<String> ids) {
      List<Tbmedicines> list = this.tbmedicinesMapper.selectBatchIds(ids);
      return list;
   }
   @Override
   public void exportData(HttpServletResponse response) {
   List<Tbmedicines> list = this.selectPage(new Page<Tbmedicines>(1, 10000), new TbmedicinesQueryVo()).getRecords();
   List<TbmedicinesExportVo> expList = new ArrayList<>();
   list.forEach(po -> {
       TbmedicinesExportVo vo = new TbmedicinesExportVo();
       BeanUtils.copyProperties(po, vo);
          expList.add(vo);
      });
      try {
          this.excelHandler.exportExcel(response, expList, TbmedicinesExportVo.class, "药品信息数据", "药品信息数据");
      } catch (Exception e) {
          e.printStackTrace();
          throw new LanfException(9005, "导出失败");
      }
   }
}
